package emulator.hardware.io.peripherals;

import java.util.LinkedList;

public class VicTapeTrack {
	long max_counter = 0;
	LinkedList<SignalEvent> event_list = new LinkedList<SignalEvent>();
	int current_read_index = 0;

	public void recordSignal(long counter, boolean signal) {
		event_list.add(new SignalEvent(counter, signal));
		if (counter > max_counter)
			max_counter = counter;
	}

	public boolean playSignal(long counter) {
		boolean signal = false;
		if (event_list.size() > 0)
		{
			if (event_list.get(current_read_index).getCounter() > counter)
			{
				if (current_read_index == 0)
					return signal;
				current_read_index = 0;
			}
			while (event_list.get(current_read_index).getCounter() < counter)
			{
				if (current_read_index+1 == event_list.size())
					break;
				current_read_index ++;
			}
			signal = event_list.get(current_read_index).getSignal();
		}
		return signal;
	}

	public long getCount() {
		return max_counter;
	}
	
	class SignalEvent
	{
		private long counter;
		private boolean signal;
		
		public SignalEvent(long counter, boolean signal) {
			this.counter = counter;
			this.signal = signal;
		}
		/**
		 * @return the counter
		 */
		public long getCounter() {
			return counter;
		}
		/**
		 * @param counter the counter to set
		 */
		public void setCounter(long counter) {
			this.counter = counter;
		}
		/**
		 * @return the signal
		 */
		public boolean getSignal() {
			return signal;
		}
		/**
		 * @param signal the signal to set
		 */
		public void setSignal(boolean signal) {
			this.signal = signal;
		}
	}
}
